require "findpath"
require "helper"
require "gui"
require "guiFunctions"

pause = true
angleText = 0
robots = {}
static = {}
terrain = {}

waypoints = {}
chosenPath = {}
moonsPath = {}
moonsDist = 0
finalPath = {}
finalDist = 0

currentPoints = {}
highlightPath = {}
successfulPoints = {}

lights = {}
particles = {}
intensity = 100
selectedRobot = {}
kMatrix = {}

dragBox = true
startXX = 0
startYY = 0
endXX = 0
endYY = 0

pathFound = false

function love.load()
    -- Fat lines.
    love.graphics.setLineWidth(2)
	love.graphics.setMode( 800, 700, false)
	
    -- Load images.
    images = {
        robot = love.graphics.newImage("robot1.png"),
        light = love.graphics.newImage("waypoint.png"),
        explode = love.graphics.newImage("explode1.png")
    }

    world = love.physics.newWorld(0,0,2000, 2000)

	gui = createGUIcontroller() -- gui is the name of the guiController
	
	createGUI(gui) --create all the textboxes and buttons
	
	avoidLights = false
	mouseLight = false
	
	if mouseLight == true then
		mainLight = createLight(0,0,100)
	end
	
	
	--createSquare(0,200,60,600)	
	--createSquare(200,-1000,60,1500)	
	--createSquare(400,200,60,600)	

	--createSquare(0,660,500,60)	
		
	--findPath(0,0,500,300)
	--startTree(0,0,500,300)

end

function createGUI(ctrl)
	createTextbox(ctrl,60,600,40,20,"Start X",40)
	createTextbox(ctrl,60,625,40,20,"Start Y",40)
	
	createTextbox(ctrl,160,600,40,20,"End X",600)
	createTextbox(ctrl,160,625,40,20,"End Y",100)

	
	createButton(ctrl,320,600,20,"Clear Path", clearPath)
	createButton(ctrl,320,625,20,"Clear Squares", clearSquares)
	createButton(ctrl,320,650,20,"Draw Path", drawPath)
	
	createButton(ctrl,520,650,20,"Drag To Place", toggleDrag)

end

function createWayPoint(x,y)
	local t = {}
	t.x = x
	t.y = y
	t.angle = 0
	
	t.ox = 16
	t.oy = 16
	t.i = images.light

	t.pathLength = 0
	t.children = {}
	t.next = {}
	t.prev = {}
	t.ttl = 80
	
	t.successfulPath = false
	
	table.insert(waypoints,t)
	t.index = #waypoints
	return t
end

function createSquare(x,y,w,h)
	local t = {}
	t.type = 'square'
	t.x = x
	t.y = y
	t.x2 = x+w
	t.y2 = y+h	
	
	t.h = h	
	t.w = w	
	table.insert(terrain,t)
end

function love.update(dt)
	if mouseLight == true then
		mainLight.x = love.mouse.getX()
		mainLight.y = love.mouse.getY()
	end
	
	--gui object handling is in other files
	handleTextBoxes(gui)
	handleButtons(gui)
	
	runPath(endXX,endYY)
	
	if love.keyboard.isDown('escape') then
        love.event.push('q') -- quit the game
    end 
	
	--for explosions
	for i,v in ipairs(particles) do
		v:update(dt)
	end
end

--handle all of the drawing
function love.draw()
	love.graphics.setColor(255,255,0)
	
	love.graphics.setColor(126,126,255)
	for i,v in ipairs(moonsPath) do
		love.graphics.circle("fill", v.x, v.y, 5)
		if moonsPath[i+1] ~= nil then
			love.graphics.line(v.x, v.y, moonsPath[i+1].x, moonsPath[i+1].y)
		else
			love.graphics.print(moonsDist, v.x, v.y+30)
		end
	end
	
	
	love.graphics.setColor(0,255,255)
	for i,v in ipairs(finalPath) do
		love.graphics.circle("fill", v.x, v.y, 5)
		if finalPath[i+1] ~= nil then
			love.graphics.line(v.x, v.y, finalPath[i+1].x, finalPath[i+1].y)
		else
			love.graphics.print(finalDist, v.x, v.y+15)
		end
	end

	love.graphics.setColor(255,0,0)
	for i,v in ipairs(terrain) do
		love.graphics.rectangle("fill",v.x, v.y, v.w,v.h)
    end
    
	if dragx1 ~= -1 and dragy1 ~= -1 then
		love.graphics.rectangle("line",dragx1, dragy1, love.mouse.getX()-dragx1,love.mouse.getY()-dragy1)
	end	
	
	drawGUI(gui)
end
